package com.gzgy.ustore.api.service.impl;


import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.dao.OrganizationDao;
import com.gzgy.ustore.api.model.OrgTreeList;
import com.gzgy.ustore.api.service.OrganizationService;
import com.ustore.model.Organization;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public List<OrgTreeList> orgListTree(Integer companyId) {

        List<OrgTreeList> trees  = new ArrayList<OrgTreeList>();
        //list 转 tree  根节点， high code  = ""
        List<OrgTreeList> treeNodes = organizationDao.orgTreeList(companyId);
        for (OrgTreeList treeNode : treeNodes) {

            if (StringUtils.isEmpty(treeNode.getHighOrg())) {
                treeNode.setOpen(true);
                trees.add(treeNode);
            }
            for (OrgTreeList it : treeNodes) {
                if (it.getHighOrg().equals(treeNode.getOrgId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<OrgTreeList>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return trees;
    }

    @Override
    public String addOrganization(Organization organization) {
        try {
            return organizationDao.add(organization).getId().toString();
        }catch (Exception e){
            //e.printStackTrace();
            if(e.getMessage().indexOf("Duplicate entry") !=-1){
                int errorCode = ExceptionCodeStatusEnum.ORG_IS_EXITS_EXCEPTION.toInt();
                String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
                throw new BaseAPIException(errorMsg,errorCode);
            }else{
                throw new BaseAPIException("系统繁忙,请稍后再试");
            }
        }
    }

    @Override
    public Organization getOrgById(String id) throws SQLException {
        Organization o = organizationDao.get(id);
        if(o == null){
            int errorCode = ExceptionCodeStatusEnum.ORG_NOT_FOUND_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
            throw new BaseAPIException(errorMsg,errorCode);
        }
        return o;
    }

    @Override
    public String orgOperation(Organization organization,String flag) throws SQLException {

        Organization o = organizationDao.get(organization.getId());
        if(o == null){
            int errorCode = ExceptionCodeStatusEnum.ORG_NOT_FOUND_EXCEPTION.toInt();
            String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
            throw new BaseAPIException(errorMsg,errorCode);
        }
        if("0".equals(flag)) {
            o.setOrgType(organization.getOrgType());
            o.setOrgName(organization.getOrgName());
            o.setPhone(organization.getPhone());
            o.setPrincipal(organization.getPrincipal());
            o.setAddress(organization.getAddress());
            o.setArea(organization.getArea());
            o.setCity(organization.getCity());
            o.setProvince(organization.getProvince());
        }else{
            o.setIsDeleted(true);
        }
        o.setUpdatedTime(organization.getUpdatedTime());
        o.setUpdatedBy(organization.getUpdatedBy());
        organizationDao.update(o);

        return "操作成功";
    }
}
